package cn.zdm.demo.config.datasource;

import com.alibaba.druid.pool.DruidDataSource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;
import java.util.Properties;

/**
 * copyright (C), 2021, 武大吉奥科技有限公司
 *
 * @Program demo
 * @Description
 * 数据源配置类
 * @Author ZhaoDeman
 * @Create 2021/3/1 20:39
 * @Version 1.0.0
 * <author>                <time>                  <version>                   <description>
 * ZhaoDeman        2021/3/1 20:39                 1.0
 */
@Slf4j
@Configuration
@Primary
public class DruidDataSourceConfig {
    /***
     * 配置数据源bean,将bean注入Spring容器中
     */
    /**
     * 用户名
     */
    @Value("${spring.datasource.username}")
    private String userName;
    /***
     * 连接密码
     */
    @Value("${spring.datasource.password}")
    private String password;
    /***
     * 连接驱动
     */
    @Value("${spring.datasource.driver-class-name}")
    private String driverClassName;
    /**
     * 连接url
     */
    @Value("${spring.datasource.url}")
    private String jdbcUrl;
    /**
     * 初始化大小
     */
    @Value("${spring.datasource.initialSize}")
    private int initialSize;
    /**
     * 空闲等待时间
     */
    @Value("${spring.datasource.minIdle}")
    private int minIdle;
    /**
     * 最大活跃
     */
    @Value("${spring.datasource.maxActive}")
    private int maxActive;
    /**
     * maxWait
     */
    @Value("${spring.datasource.maxWait}")
    private int maxWait;
    /**
     * timeBetweenEvictionRunsMillis
     */
    @Value("${spring.datasource.timeBetweenEvictionRunsMillis}")
    private int timeBetweenEvictionRunsMillis;
    /**
     * minEvictableIdleTimeMillis
     */
    @Value("${spring.datasource.minEvictableIdleTimeMillis}")
    private int minEvictableIdleTimeMillis;
    /**
     * validationQuery
     */
    @Value("${spring.datasource.validationQuery}")
    private String validationQuery;
    /**
     * testWhileIdle
     */
    @Value("${spring.datasource.testWhileIdle}")
    private boolean testWhileIdle;
    /**
     * testOnBorrow
     */
    @Value("${spring.datasource.testOnBorrow}")
    private boolean testOnBorrow;
    /**
     * testOnReturn
     */
    @Value("${spring.datasource.testOnReturn}")
    private boolean testOnReturn;
    /**
     * poolPreparedStatements
     */
    @Value("${spring.datasource.poolPreparedStatements}")
    private boolean poolPreparedStatements;
    /**
     * maxPoolPreparedStatementPerConnectionSize
     */
    @Value("${spring.datasource.maxPoolPreparedStatementPerConnectionSize}")
    private int maxPoolPreparedStatementPerConnectionSize;
    /**
     * filters
     */
    @Value("${spring.datasource.filters}")
    private String filters;
    /**
     * connectionProperties
     */
    @Value("${spring.datasource.connectionProperties}")
    private Properties connectionProperties;
    /**
     * sqlScriptEncoding
     */
    @Value("${spring.datasource.sql-script-encoding}")
    private String sqlScriptEncoding;
    /**
     * useGlobalDataSourceStat
     */
    @Value("${spring.datasource.useGlobalDataSourceStat}")
    private boolean useGlobalDataSourceStat;

    @Bean
    public DataSource dataSource() {
        DruidDataSource druidDataSource = new DruidDataSource();
        log.info("=========>{}", password);
        druidDataSource.setUrl(jdbcUrl);
        druidDataSource.setUsername(userName);
        druidDataSource.setPassword(password);
        druidDataSource.setDriverClassName(driverClassName);
        druidDataSource.setMaxActive(maxActive);
        druidDataSource.setInitialSize(initialSize);
        druidDataSource.setMinIdle(minIdle);
        druidDataSource.setMaxWait(maxWait);
        druidDataSource.setTimeBetweenConnectErrorMillis(timeBetweenEvictionRunsMillis);
        druidDataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
        druidDataSource.setValidationQuery(validationQuery);
        druidDataSource.setTestOnBorrow(testOnBorrow);
        druidDataSource.setTestWhileIdle(testWhileIdle);
        druidDataSource.setTestOnReturn(testOnReturn);
        druidDataSource.setPoolPreparedStatements(poolPreparedStatements);
        druidDataSource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
        try {
            druidDataSource.setFilters(filters);
        } catch (Exception e) {
            log.error("数据库连接池，filter失败", e);
        }
        druidDataSource.setUseGlobalDataSourceStat(useGlobalDataSourceStat);
        return druidDataSource;
    }
}


